package abg.core.service.mt.impl;


import abg.core.common.util.StringKit;
import abg.core.domain.db.MtRoomMsg;
import abg.core.domain.db.SysUser;
import abg.core.domain.mt.MtRoomMsgVo;
import abg.core.domain.search.Psh;
import abg.core.mapper.MtRoomMsgMapper;
import abg.core.service.mt.MtRoomMsgService;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import static abg.core.domain.db.table.MtRoomMsgTableDef.MT_ROOM_MSG;
import static abg.core.domain.db.table.SysUserTableDef.SYS_USER;

/**
 * 会议室消息
 *
 * @author leyili
 * @since 2023-10-5 15:31:24
 */
@Slf4j
@Service
public class MtRoomMsgServiceImpl implements MtRoomMsgService {

    @Resource
    private MtRoomMsgMapper mtRoomMsgMapper;

    @Override
    public Page<MtRoomMsgVo> page(MtRoomMsgVo mtRoomMsgVo, Psh psh) {

        QueryWrapper wrapper = QueryWrapper.create()
                .select(MT_ROOM_MSG.ID, MT_ROOM_MSG.USER_ID, MT_ROOM_MSG.CONTENT, MT_ROOM_MSG.CT)
                .from(MtRoomMsg.class)
                .leftJoin(SysUser.class)
                .on(MT_ROOM_MSG.USER_ID.eq(SYS_USER.ID))
                .select(SYS_USER.ACCOUNT, SYS_USER.AVATAR, SYS_USER.NICK_NAME)
                // 根据 用户ID 查询
                .where(MT_ROOM_MSG.USER_ID.eq(mtRoomMsgVo.getUserId()))
                // 根据用户账号查询
                .and(SYS_USER.ACCOUNT.eq(mtRoomMsgVo.getAccount()))
                // 创建时间倒序
                .orderBy(MT_ROOM_MSG.CT.desc());

        // 查询早于传入时间的
        if (StringKit.isNotBlank(psh.getTime1())) {
            wrapper.and(MT_ROOM_MSG.CT.lt(psh.getTime1()));
        }

        return mtRoomMsgMapper.paginateAs(psh, wrapper, MtRoomMsgVo.class);

    }
}